package com.leenleda.ips.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.leenleda.ips.MyApplication;


public class UShared {
    //****************SharedPreferences******************
    public static final boolean putString(Context context, String key, String value) {
        value = value == null ? "" : value;
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putString(key, value);
        boolean result = editor.commit();
        if (!result) {
            return false;
        }
        return true;
    }

    public static final boolean putString(String key, String value) {
        value = value == null ? "" : value;
        SharedPreferences.Editor editor = getSharedPreference(MyApplication.getInstance()).edit();
        editor.putString(key, value);
        boolean result = editor.commit();
        if (!result) {
            return false;
        }
        return true;
    }

    private static final SharedPreferences getSharedPreference(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static final String getString(Context context, String key) {
        return getSharedPreference(context).getString(key, "");
    }

    public static final String getString(String key) {
        return getSharedPreference(MyApplication.getInstance()).getString(key, "");
    }

    public static final String getValue(Context context, String key) {
        return getSharedPreference(context).getString(key, "");
    }

    public static final boolean putInt(Context context, String key,
                                       int value) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putInt(key, value);
        boolean result = editor.commit();
        if (!result) {
            return false;
        }
        return true;
    }

    public static final boolean putInt(String key, int value) {
        SharedPreferences.Editor editor = getSharedPreference(MyApplication.getInstance()).edit();
        editor.putInt(key, value);
        boolean result = editor.commit();
        if (!result) {
            return false;
        }
        return true;
    }

    public static final long getLong(String key,
                                     long default_data) {
        return getSharedPreference(MyApplication.getInstance()).getLong(key, default_data);
    }

    public static final long getLong(Context context, String key,
                                     long default_data) {
        return getSharedPreference(context).getLong(key, default_data);
    }

    public static final boolean putLong(String key, Long value) {
        SharedPreferences.Editor editor = getSharedPreference(MyApplication.getInstance().getApplicationContext()).edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public static final boolean putLong(Context context, String key, Long value) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public static final int getIntValue(Context context, String key) {
        return getSharedPreference(MyApplication.getInstance()).getInt(key, 0);
    }

    public static final int getIntValue(String key) {
        return getSharedPreference(MyApplication.getInstance()).getInt(key, -1);
    }

    public static final int getInt50(String key) {
        return getSharedPreference(MyApplication.getInstance()).getInt(key, 50);
    }

    public static final Boolean getBooleanValue(Context context, String key) {
        return getSharedPreference(MyApplication.getInstance()).getBoolean(key, false);
    }

    public static final Boolean getBooleanValue(String key) {
        return getSharedPreference(MyApplication.getInstance()).getBoolean(key, false);
    }

    public static final Boolean getBooleanDefault(String key) {
        return getSharedPreference(MyApplication.getInstance()).getBoolean(key, true);
    }

    public static final void putBooleanValue(String key, boolean bl) {
        SharedPreferences.Editor edit = getSharedPreference(MyApplication.getInstance()).edit();
        edit.putBoolean(key, bl);
        edit.commit();
    }
    //****************SharedPreferences******************
}
